<?php


namespace App\Http\Controllers\Api\ClassEr;

use App\Services\ClassEr\ClassServices;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ClassController extends Controller
{
    /**
     * 获取班级信息
     *
     * @param Request            $request
     * @param ClassServices      $ClassServices
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function list(Request $request,ClassServices  $ClassServices)
    {

        $data = $ClassServices->getList();
        if ($data === false) {
            return json_encode([]);
        }
        return json_encode($data);
    }

    /**
     * 新增班级信息
     *
     * @param Request            $request
     * @param ClassServices      $ClassServices
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function add(Request $request,ClassServices  $ClassServices)
    {

        $this->validate($request, [
            'name' => 'required|string',
        ]);
        $data = $ClassServices->add($request->all());
        if ($data === false) {
            return '操作失败';
        }
        return '操作成功';
    }

    /**
     * 编辑班级信息
     *
     * @param Request            $request
     * @param ClassServices      $ClassServices
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function edit(Request $request,ClassServices  $ClassServices)
    {

        $this->validate($request, [
            'id' => 'required|integer',
            'name' => 'required|string',
        ]);
        $data = $ClassServices->edit($request->all());
        if ($data === false) {
            return '操作失败';
        }
        return '操作成功';
    }


    /**
     * 删除班级信息
     *
     * @param Request            $request
     * @param ClassServices      $ClassServices
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function del(Request $request,ClassServices  $ClassServices)
    {

        $this->validate($request, [
            'id' => 'required|integer'
        ]);
        $data = $ClassServices->del($request->all());
        if ($data === false) {
            return '操作失败';
        }
        return '操作成功';
    }

}
